package yxy.game.pm2.bean.module.rank.openserviceactive;

import org.joda.time.DateTime;
import org.redisson.api.RMap;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.BaseGameRedisLockBean;

/**
 * 记录第一名完成任务的玩家信息
 *
 * @ClassName RecordTaskFirstPlayrerTools
 * @Author jingtao-zheng
 * @Date 2022/7/11 15:39
 * @Version 1.0
 **/
public class RecordTaskFirstPlayrerBean extends BaseGameRedisLockBean {
    private int taskid;//任务id
    private String playercode;//玩家code
    private DateTime firstcompletetime;//第一名完成时间
    private int type;//活动类型

    public int getTaskid() {
        return taskid;
    }

    public void setTaskid(int taskid) {
        this.taskid = taskid;
    }

    public String getPlayercode() {
        return playercode;
    }

    public void setPlayercode(String playercode) {
        this.playercode = playercode;
    }

    public DateTime getFirstcompletetime() {
        return firstcompletetime;
    }

    public void setFirstcompletetime(DateTime firstcompletetime) {
        this.firstcompletetime = firstcompletetime;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }


    //---------------------------------------------------------------记录

    public static void record(DateTime newTime, String playerCode, int taskId, int acttype) {
        try {
            RMap<String, RecordTaskFirstPlayrerBean> RTFPmap = get(key(playerCode, acttype));
            RecordTaskFirstPlayrerBean RTFPbean = RTFPmap.get(String.valueOf(taskId));
            //如果缓存查到数据为空就说明还没有记录过
            if(null == RTFPbean){
                //开始初始化第一名完成任务玩家信息
                RecordTaskFirstPlayrerBean bean = new RecordTaskFirstPlayrerBean();
                bean.setFirstcompletetime(newTime);
                bean.setPlayercode(playerCode);
                bean.setTaskid(taskId);
                bean.setType(acttype);
                //记录
                bean.set();
            }
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    private void set(){
        try {
            RMap<String, RecordTaskFirstPlayrerBean> rmap = get(key());
            rmap.put(String.valueOf(getTaskid()),this);
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    static public RMap<String, RecordTaskFirstPlayrerBean> get(String key) throws RedisException {
        RMap<String, RecordTaskFirstPlayrerBean> map = getMapFromRedis(key);
        return map;
    }

    static private RMap<String, RecordTaskFirstPlayrerBean> getMapFromRedis(String key) throws RedisException {
        return RedisManager.client(redis()).getMap(key);
    }

    public static String key(String playercode, int type) {
        AreaBean ab = AreaModule.area(playercode);
        return String.format("AREA:%s:RANK:OSRUSH:%s:RECORD", ab.getAreaId(), type);
    }

    private String key() {
        AreaBean ab = AreaModule.area(getPlayercode());
        return String.format("AREA:%s:RANK:OSRUSH:%s:RECORD", ab.getAreaId(), getType());
    }
}
